package com.xinchuang.service.targetSystem.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultEnum;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.mapper.targetSystem.WeaponMapper;
import com.xinchuang.entity.targetSystem.WeaponData;
import com.xinchuang.service.targetSystem.WeaponService;
import com.xinchuang.util.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WeaponServiceImpl
 * @Description: TODO(武器业务处理)
 * @Author zhangxuan
 * @Date 2022-03-03 11:49
 * @Verion V1.0
 */
@Service
public class WeaponServiceImpl implements WeaponService {

    @Resource
    private WeaponMapper weaponMapper;

    private SnowflakeIdWorker snow = new SnowflakeIdWorker(0,0);

    @Override
    public Result addWeapon(String id, String weaponName, String description) {
        if (StringUtils.isBlank(weaponName)) {
            return ResultUtil.error(ResultEnum.WEAPON_NAME_NOT_NULL);
        }
        String name = weaponMapper.selectWeaponNameByName(weaponName);
        if (StringUtils.isNotBlank(name) && !name.equals(id)) {
            return ResultUtil.error(ResultEnum.WEAPON_NAME_EXIST);
        }
        WeaponData weaponData = new WeaponData();
        weaponData.setWeaponName(weaponName);
        weaponData.setRemark(description);
        synchronized (this) {
            if (StringUtils.isNotBlank(id) && !id.equals("0")) {
                weaponData.setId(id);
                weaponMapper.addWeaponAndId(weaponData);
            } else {
                weaponData.setId(snow.nextId() + "");
                weaponMapper.addWeaponAndId(weaponData);
            }
        }
        return ResultUtil.success();
    }

    @Override
    public Result deleteWeapon(String id) {
        int delete = weaponMapper.deleteWeapon(id);
        if (delete > 0) {
            return ResultUtil.success("删除成功", null);
        }
        return ResultUtil.error(ResultEnum.DELETE_EMPTY_ERROR);
    }

    @Override
    public Result selectWeaponList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<WeaponData> list = weaponMapper.selectWeaponList();
        PageInfo<WeaponData> pageInfo = new PageInfo<>(list);
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> resultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(pageInfo.getList())) {
            for (int i = 0; i < pageInfo.getList().size(); i++) {
                WeaponData weaponData =  pageInfo.getList().get(i);
                Map<String, Object> map = new HashMap<>();
                map.put("number", ((pageNum - 1) * pageSize) + i + 1);
                map.put("id", weaponData.getId());
                map.put("weaponName", weaponData.getWeaponName());
                map.put("description", weaponData.getRemark());
                resultList.add(map);
            }
        }
        resultMap.put("weaponList", resultList);
        resultMap.put("pageNum", pageInfo.getPageNum());
        resultMap.put("pageSize", pageInfo.getPageSize());
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("pageAll", pageInfo.getPages());
        return ResultUtil.success(resultMap);
    }

    @Override
    public Result selectWeaponPut() {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<WeaponData> list = weaponMapper.selectWeaponList();
        if (CollectionUtil.isNotEmpty(list)) {
            for (WeaponData weaponData : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("value", weaponData.getId());
                map.put("label", weaponData.getWeaponName());
                resultList.add(map);
            }
        }
        return ResultUtil.success(resultList);
    }
}